import java.io.File;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-09-14
 * Time: 19:11
 */
public class demo1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要搜索的目录:");
        String rootDir = scanner.next();
        File rootFile = new File(rootDir);
        if(!rootFile.isDirectory()){
            System.out.println("输入的不是目录:");
            return;
        }
        System.out.println("请输入关键字:");
        String keyword = scanner.next();
        scanDir(rootFile, keyword);
    }
    private static void scanDir(File rootFile, String keyword){
        File[] files = rootFile.listFiles();
        if(files == null){
            return;
        }
        for(File file : files){
            if(file.isFile()){
                dealFile(file, keyword);
            }else{
                scanDir(file, keyword);
            }
        }
    }
    private static void dealFile(File file, String keyword){
        if(file.getName().contains(keyword)){
            System.out.println("发现文件:" + file.getAbsoluteFile() + "包含关键字!是否删除?(y/n)");
            Scanner scanner = new Scanner(System.in);
            String input = scanner.next();
            if(input.equalsIgnoreCase("y")){
                file.delete();
                System.out.println("文件已删除!");
            }
        }
    }
}
